using System.Drawing;
using FarPoint.Win;
using FarPoint.Win.Spread;

namespace Spread.Sparkline.Features
{
    public partial class BarSparkline : Spread.Common.Features.Sample.BaseSampleControl
    {
        public BarSparkline()
        {
            InitializeComponent();
            Name = "HBar and VBar";

            InitSpread();
        }

        #region Private methods
        private void InitSpread()
        {
            fpSpread1.Sheets.Count = 2;
            fpSpread1.TabStripInsertTab = false;

            InitHorizontalSparkline(fpSpread1.Sheets[0], "Horizontal");
            InitVerticalSparkline(fpSpread1.Sheets[1], "Vertical");
        }

        private void InitHorizontalSparkline(SheetView sheetView, string name)
        {
            var worksheet = sheetView.AsWorksheet();
            worksheet.Name = name;
            worksheet.SetValue(1, 0, new object[,]
            {
                { "Channel", "Satisfaction %", "Diagram" },
                { "Email", 0.92, null },
                { "Social Networks", 0.79, null },
                { "Forums/Message Boards", 0.65, null },
                { "Phone", 0.58, null },
                { "Text Message", 0.55, null },
                { "Online Chat", 0.39, null },
                { "Self-Serve Knowledge Base", 0.28, null },
                { "On-Page Support Widget", 0.19, null }
            });

            for (int i = 3; i <= 10; i++)
            {
                worksheet.Rows[$"{i}"].RowHeight = 30;
                worksheet.Cells[$"C{i}"].Formula = GetFormula($"B{i}");
            }

            worksheet.Cells["A1:C1"].Merge();
            worksheet.Cells["A1"].Value = "Customer Service Satisfaction, by Channel";
            worksheet.Cells["A1"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 17 });
            sheetView.Cells["A1"].BackColor = ColorTranslator.FromHtml("#E8E8E8");
            worksheet.Cells["A1"].VerticalAlignment = GrapeCity.Spreadsheet.VerticalAlignment.Center;

            worksheet.Cells["A2:C2"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 13, Bold = true });
            worksheet.Cells["A2:C2"].VerticalAlignment = GrapeCity.Spreadsheet.VerticalAlignment.Center;
            worksheet.Cells["A2:C2"].Borders.ApplyBorder(new GrapeCity.Spreadsheet.Border()
            {
                Bottom = new GrapeCity.Spreadsheet.BorderLine(GrapeCity.Spreadsheet.BorderLineStyle.Thin, GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Black))
            });

            worksheet.Cells["B3:B10"].NumberFormat = "0%";
            worksheet.Cells["B3:B10"].HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.Left;

            worksheet.Rows["1:2"].RowHeight = 35;
            worksheet.Columns["A"].ColumnWidth = 250;
            worksheet.Columns["B"].ColumnWidth = 120;
            worksheet.Columns["C"].ColumnWidth = 250;
        }

        private void InitVerticalSparkline(SheetView sheetView, string name)
        {
            var worksheet = sheetView.AsWorksheet();
            worksheet.Name = name;
            worksheet.SetValue(1, 0, new object[,]
            {
                { "Name", "Employee 1", "Employee 2", "Employee 3", "Employee 4", "Employee 5", "Employee 6", "Employee 7", "Employee 8" },
                { "Progress", 1.1, 0.79, 0.59, 0.55, 0.37, 0.19, 0.35, 0.60 }
            });

            for (int i = 0; i < 8; i++)
            {
                char columnChar = (char)(66 + i);
                worksheet.Columns[$"{columnChar}"].ColumnWidth = 80;
                worksheet.Cells[$"{columnChar}4"].Formula = GetVBarFormula(columnChar + "3");
            }

            worksheet.Cells["A1:I1"].Merge();
            worksheet.Cells["A1"].Value = "Sprint 3 Progress";
            worksheet.Cells["A1"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 17 });
            sheetView.Cells["A1"].BackColor = ColorTranslator.FromHtml("#E8E8E8");
            worksheet.Cells["A1"].VerticalAlignment = GrapeCity.Spreadsheet.VerticalAlignment.Center;

            worksheet.Cells["A2:A3"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 17, Bold = true });
            worksheet.Cells["A2:A3"].VerticalAlignment = GrapeCity.Spreadsheet.VerticalAlignment.Center;
            worksheet.Cells["A2:A3"].Borders.ApplyBorder(new GrapeCity.Spreadsheet.Border()
            {
                Right = new GrapeCity.Spreadsheet.BorderLine(GrapeCity.Spreadsheet.BorderLineStyle.Thin, GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Black))
            });

            worksheet.Cells["B3:I3"].NumberFormat = "0%";
            worksheet.Cells["B3:I3"].HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.Left;

            worksheet.Rows["1:3"].RowHeight = 40;
            worksheet.Rows["4"].RowHeight = 220;
            worksheet.Columns["A"].ColumnWidth = 120;
        }

        private string GetFormula(string range)
        {
            return "IF(" + range + ">=0.8,HBARSPARKLINE(" + range + ",\"#092834\",true," + range + "), " +
                "IF(" + range + ">=0.6,HBARSPARKLINE(" + range + ",\"#347B98\",true," + range + "), " +
                "IF(" + range + ">=0.4,HBARSPARKLINE(" + range + ",\"#66B032\",true," + range + "), " +
                "IF(" + range + ">=0.2,HBARSPARKLINE(" + range + ",\"#B2D732\",true," + range + "), " +
                "IF(" + range + ">=0,HBARSPARKLINE(" + range + ",\"#F0F7D4\",true," + range + "), HBARSPARKLINE(" + range + ",\"red\") " + ")))))";
        }

        private string GetVBarFormula(string range)
        {
            return "IF(" + range + ">=0.8,VBARSPARKLINE(" + range + ",\"#092834\",true,0.4), " +
                "IF(" + range + ">=0.6,VBARSPARKLINE(" + range + ",\"#347B98\",true,0.4), " +
                "IF(" + range + ">=0.4,VBARSPARKLINE(" + range + ",\"#66B032\",true,0.4), " +
                "IF(" + range + ">=0.2,VBARSPARKLINE(" + range + ",\"#B2D732\",true,0.4), " +
                "IF(" + range + ">=0,VBARSPARKLINE(" + range + ",\"#F0F7D4\",true,0.4), VBARSPARKLINE(" + range + ",\"red\") " + ")))))";
        }
        #endregion
    }
}


//
//BarSparkline.Designer.cs
//



namespace Spread.Sparkline.Features
{
    partial class BarSparkline
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BarSparkline));
            this.fpSpread1 = new FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None, ((object)(resources.GetObject("pnlMain.Controls"))));
            this.fpSpread1_Sheet1 = this.fpSpread1.GetSheet(0);
            this.pnlMain.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread1);
            // 
            // pnlControls
            // 
            this.pnlControls.Size = new System.Drawing.Size(210, 599);
            // 
            // fpSpread1
            // 
            this.fpSpread1.AccessibleDescription = "Book1, Sheet1, Row 0, Column 0";
            this.fpSpread1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread1.Font = new System.Drawing.Font("Calibri", 11F);
            this.fpSpread1.Location = new System.Drawing.Point(0, 0);
            this.fpSpread1.Name = "fpSpread1";
            this.fpSpread1.Size = new System.Drawing.Size(636, 599);
            this.fpSpread1.TabIndex = 3;
            // 
            // BarSparkline
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "BarSparkline";
            this.pnlMain.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion
        private FarPoint.Win.Spread.FpSpread fpSpread1;
        private FarPoint.Win.Spread.SheetView fpSpread1_Sheet1;
    }
}
